//
//  ViewController.m
//  WKProxy
//
//  Created by Bennie on 16/4/21.
//  Copyright © 2016年 Bennett. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <RNCachingURLProtocol.h>
#import "NSURLProtocolProxy.h"
#import <ReactiveCocoa.h>
#import <JRSwizzle.h>
#import "VAProtocolHook.h"

@interface ViewController ()<WKUIDelegate ,WKNavigationDelegate ,WKScriptMessageHandler>

@property (nonatomic,strong) WKWebView *webView;

@end

@implementation ViewController

- (WKWebView*)webView {
    if (!_webView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"ajaxHandler" withExtension:@"js"];
        NSString *ajaxHandler = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
        WKUserScript *ajaxScript = [[WKUserScript alloc] initWithSource:ajaxHandler injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
        [config.userContentController addUserScript:ajaxScript ];
        [config.userContentController addScriptMessageHandler:self name:@"callbackHandler"];
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    [NSURLProtocol registerClass:[RNCachingURLProtocol class]];
    [NSURLProtocol registerClass:[NSURLProtocolProxy class]];
    
//    config.protocolClasses = @[[RNCachingURLProtocol class]];
    
//    [RACObserve([NSURLSession sharedSession], configuration) subscribeNext:^(id x) {
//        NSLog(@"configuration---->%@" ,x);
//    }];
//    
//    [[[NSURLSession sharedSession].configuration rac_signalForSelector:@selector(setProtocolClasses:)] subscribeNext:^(id x) {
//        NSLog(@"setProtocolClasses---->%@" ,x);
//    }];
    
    NSURLSessionConfiguration *ddd = [[NSURLSession sharedSession] configuration];
    id share = [ddd valueForKeyPath:@"_proxySession"];
    
//    Class dataProtocol = NSClassFromString(@"_NSURLDataProtocol");
    Class httpProtocol = NSClassFromString(@"_NSURLHTTPProtocol");
    
//    [NSURLSession sharedSession].configuration.protocolClasses = @[[RNCachingURLProtocol class]];
    
    self.webView.navigationDelegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.vip.com"]]];
    
//    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
//    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"http://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        
//    }];
//    [task resume];
    
    [self.view addSubview:self.webView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.webView.frame = self.view.bounds;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
